package com.ibm.risk.irmp.base.auth.rest;

import com.ibm.risk.irmp.base.auth.service.IOrganizationService;
import com.ibm.risk.irmp.base.dict.vo.BizBranchParamVo;
import com.ibm.risk.irmp.common.auth.vo.OrgInfoTree;
import com.ibm.risk.irmp.common.auth.vo.OrgInfoVo;
import com.ibm.risk.irmp.common.vo.PagedResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/v2/auth/org")
public class OrganizationController {

    @Autowired
    private IOrganizationService organizationService;

    @GetMapping("/bizBranches/by-name-code")
    public PagedResult<OrgInfoVo> findBizBranchList(BizBranchParamVo vo){
        return organizationService.findBizBranchList(vo);
    }

    @GetMapping("/bizBranches/by-parent")
    public List<OrgInfoVo> getOrgListByParent(@RequestParam(required = false) String parent) {
        return organizationService.getOrgListByParentId(parent);
    }

    /**
     * 保存机构角色关联关系
     * @param branchCode 机构编码
     * @param roles 角色编码列表
     */
    @PostMapping("/bizBranches/roles/{branchCode}")
    public void saveBizBranchRoles(@PathVariable String branchCode, @RequestBody List<String> roles) {
        organizationService.saveBizBranchRoles(branchCode, roles);
    }

    /**
     * 保存机构角色关联关系
     * @param branchCode 机构编码
     * @return  roles 角色编码列表
     */
    @GetMapping("/bizBranches/roles/{branchCode}")
    public List<String> saveBizBranchRoles(@PathVariable String branchCode) {
        return organizationService.getBizBranchRoles(branchCode);
    }

    @GetMapping("/bizBranches/tree")
    public List<? extends OrgInfoTree> getBizOrgTree() {
        return organizationService.getBizOrgTree();
    }
}
